from tkinter import *
from tkinter import messagebox
import time, datetime
from ExcelStyle import ExcelStyle
import pandas as pd
from openpyxl import load_workbook


def update_time():
    clock_label.configure(text=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), font="微软雅黑")
    clock_label.after(1000, update_time)


def task_input():
    task_value = task_txt.get()
    task_txt.delete(0, END)
    start = time.time()
    start1 = datetime.datetime.now()
    start2 = start1.strftime('%Y/%m/%d %H:%M:%S')

    messagebox.showinfo('任务', '结束任务')

    end = time.time()
    end1 = start1 + datetime.timedelta(seconds=end - start)
    end2 = end1.strftime('%Y/%m/%d %H:%M:%S')
    last.setdefault(task_value, [start2, end2, str(round(end - start)) + " Secs", str(round((end - start) / 60)) + " Mins"])


def stop_task():
    frame = pd.DataFrame.from_dict(last, orient="index").reset_index().rename(columns={
        'index': "Task", 0: "Start_Time", 1: "End_Time", 2: "Duration", 3: "Mins"
    })
    filename = datetime.datetime.now().strftime('-%Y-%m-%d')
    expath = r"C:\Users\Desktop\Daily_Time_Task" + filename + ".xlsx"
    frame.to_excel(expath, index=False)
    wb = load_workbook(expath)
    ws = wb.active
    estel = ExcelStyle(wb, ws, 6)
    estel.deco_firstRow()
    estel.deco_all()
    wb.save(expath)
    messagebox.showinfo('结束', '======今天任务结束======')
    root.destroy()


last = {}

root = Tk()
root.title('时间管理')
root.geometry('400x100+500+300')

task_txt = Entry(root, width=15, font="微软雅黑")
task_txt.grid(row=1, column=1)

task = Button(root, text="任务", width=15, font="微软雅黑", command=task_input)
task.grid(row=1, column=0)

stop = Button(root, text="终止", width=15, font="微软雅黑", command=stop_task)
stop.grid(row=2, column=0)

clock_label = Label(root)
clock_label.grid(row=2, column=1)
update_time()

root.mainloop()